<?php

namespace app\common\library;

/**
 *  门禁
 */
class Men
{





    /**
     *  获取token
     */
    public static function token($appId, $apiKey)
    {

        header("Content-type: text/html; charset=utf-8");
        $apiurl = "https://api.parkline.cc/api/token";

        $acsurl = "https://chaapi.dzxwbj.com/door/index";
        $ch = curl_init();
        $data["apiid"] = $appId;
        $data["apikey"] = $apiKey;
        $data = http_build_query($data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $apiurl);
        curl_setopt($ch, CURLOPT_REFERER, $acsurl);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $data = curl_exec($ch);

        //{"expires_in":"2020-09-04 00:11:00","access_token":"7NZbnhKQptOAHUXWOAuRshpjAFljYTwsBXQJ2HBQ97fmkZHRyuIAeqAiDLj1piwZlIxV0IvN5CI="}
        return json_decode($data, true);

    }


    public static function unlockdook($access_token, $devid, $typeid,$lockid)
    {
        header("Content-type: text/html; charset=utf-8");
        $apiurl = "https://api.parkline.cc/api/devicecgi";
        $acsurl = "https://chaapi.dzxwbj.com/door/index";
        $ch = curl_init();
        if ($typeid === 0) {
            $data["typeid"] = "02";
        } else {
            $data["typeid"] = '0'."{$typeid}";
        }
        if($lockid==0){
            $data["lockid"] = "01";
        }else{
            $data["lockid"] = '0'."{$lockid}";;
        }
        $data["token"] = $access_token;
        $data["devid"] = $devid;

        $data = http_build_query($data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $apiurl);
        curl_setopt($ch, CURLOPT_REFERER, $acsurl);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $data = curl_exec($ch);


        return json_decode($data, true);
    }

    /**
     *   开锁
     */
    public static function unlock($access_token, $devid)
    {
        header("Content-type: text/html; charset=utf-8");
        $apiurl = "https://api.parkline.cc/api/devicecgi";
        $acsurl = "https://chaapi.dzxwbj.com/door/index";
        $ch = curl_init();
        $data["token"] = $access_token;
        $data["typeid"] = "01";
        $data["devid"] = $devid;
        $data["lockid"] = "01";
        $data = http_build_query($data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $apiurl);
        curl_setopt($ch, CURLOPT_REFERER, $acsurl);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $data = curl_exec($ch);

        return json_decode($data, true);
    }
    /**
     *   语音
     */
    public static function unlockvoice($access_token, $devid,$voice)
    {
        header("Content-type: text/html; charset=utf-8");
        $apiurl = "https://api.parkline.cc/api/devicecgi";
        $acsurl = "https://chaapi.dzxwbj.com/door/index";
        $ch = curl_init();
        $data["token"] = $access_token;
        $data["typeid"] = "01";
        $data["devid"] = $devid;
        $data["lockid"] = '0'."$voice";

        $data = http_build_query($data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $apiurl);
        curl_setopt($ch, CURLOPT_REFERER, $acsurl);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $data = curl_exec($ch);

        return json_decode($data, true);
    }

    /**
     *  查询设备状态
     */
    public static function getlock($access_token, $devid)
    {
        header("Content-type: text/html; charset=utf-8");
        $apiurl = "https://api.parkline.cc/api/statuscgi";
        $acsurl = "http://" . $_SERVER['HTTP_HOST'];

        $ch = curl_init();
        $data["token"] = $access_token;
        $data["devid"] = $devid;
        $data["lockid"] = "01";
        $data = http_build_query($data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $apiurl);
        curl_setopt($ch, CURLOPT_REFERER, $acsurl);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $data = curl_exec($ch);
        return json_decode($data, true);
    }

    /**
     * 人脸更新
     */
    public static function facecgi($data)
    {
        $apiurl = "https://api.parkline.cc/api/facecgi";
        return static::curls($data, $apiurl);
    }

    public static function curls($data, $apiurl)
    {
        $acsurl = "http://" . $_SERVER['HTTP_HOST'];

        header("Content-type: text/html; charset=utf-8");
        $ch = curl_init();
        $data = http_build_query($data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $apiurl);
        curl_setopt($ch, CURLOPT_REFERER, $acsurl);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $data = curl_exec($ch);
        return json_decode($data, true);
    }

}



